package com.jetlion.demo3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.btn);

        //弹窗构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                builder.setTitle("Login");

                //布局服务
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);

                //加载界面
                View loginView = inflater.inflate(R.layout.view_login, null);

                builder.setView(loginView);

                EditText editUsername = loginView.findViewById(R.id.edit_username);
                EditText editPassword = loginView.findViewById(R.id.edit_password);

                //确定按钮
                builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String usernameStr = editUsername.getText().toString();
                        String passwordStr = editPassword.getText().toString();

                        Log.i("MainActivity", "username:" + usernameStr + ",password:" + passwordStr);

                        //模拟登陆判断
                        if("admin".equals(usernameStr)&&"admin123".equals(passwordStr)){
                            Toast.makeText(MainActivity.this,"登录成功！",Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(MainActivity.this,"登录失败！",Toast.LENGTH_LONG).show();
                        }
                    }
                });
                //取消按钮
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Toast.makeText(MainActivity.this, "press Cancel", Toast.LENGTH_LONG).show();
                    }
                });

                //显示弹窗
                builder.create().show();
            }
        });

    }
}